<?php
/**
 * Date: 2018/2/5
 * Time: 11:31
 * Author: zeronlee
 */

namespace App\Models\Access\User;


use Arcanedev\Support\Bases\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserAddress extends Model
{

    use softDeletes;

    protected $table = 'user_address';

    protected $primaryKey = 'id';

    public $timestamps = true;

    protected $guarded = [];

    protected $fillable = ['user_id','user_name','province','city','area','street','phone'];

    protected $dates = ['delete_at'];

    /**
     * 插入记录
     * @param mixed $request
     * @return bool
     * @throws \Exception
     */
    public function insertRow($request){

        if ($this->save($request->input())) {


            return true;
        }

        throw new \Exception('添加失败');

    }
    /**
     * 修改记录
     * @param mixed $request
     * @return bool
     * @throws \Exception
     */
    public function updateRow($request){

        $update = $this->find($request->id);

        if($update->update($request->input())){

            return true;
        }
        throw new \Exception('更新失败');
    }
    /**
     * 删除记录
     * @param mixed $request
     * @return bool
     * @throws \Exception
     */
    public function deleteRow($request){
        if (empty($request->id)){
            throw new \Exception('非法操作');
        }
        $del = $this->find($request->id);
        $del->delete();
        if ($del->trashed()) {
            return true;
        }else{

            throw new \Exception('禁用失败');
        }

    }
    /**
     * 激活
     * @param mixed $request
     * @return bool
     */
    public function activeRow($request){

        $active = $this->withTrashed()->find($request->id);
        //避免禁用用户二次进入
        if ($active){
            $active->deleted_at = NULL;
            if ($active->save()){
                return true;
            }
        }
        return false;

    }
    /**
     * 某用户收货地址
     * @param mixed $request
     * @return mixed
     * @throws \Exception
     */
    public function get($request){

        $where = ['user_id'=>$request->id];

        $address = $this->where($where)->get();

        if(!$address->isEmpty()){

            return $address;
        }

        throw new \Exception('暂无信息');
    }
}